#!/usr/bin/python2.4
#
# Copyright 2009 Google Inc. All Rights Reserved.
"""Implementation of Dummy robot."""

__author__ = 'davidbyttow@google.com (David Byttow)'


import logging

from waveapi import events
from waveapi import model
from waveapi import robot

# so people can visit qnabot from the web at large
import os
from google.appengine.ext.webapp import template

import cgi
import wsgiref.handlers

from google.appengine.ext import webapp

# Browser interface
def webmain():
  application = webapp.WSGIApplication(
                                       [('/', MainPage)
                                        ],
                                       debug=True)
  # if this is run then it seems to take over all urls, need to figure out how to let it coexist with Wave
  # wsgiref.handlers.CGIHandler().run(application)

class MainPage(webapp.RequestHandler):
  def get(self):

    template_values = {
      'robotname': 'qnabot@appspot.com',
      'description' : 'This is a Q &amp; A robot.'
      }

    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

# Wave interface

# Globals
ROBOT_NAME = 'qnabot'
LOCAL_NAME = 'imqna'


def OnSelfAdded(properties, context):
  """Invoked when Dummy is first added to the wave."""
  root_wavelet = context.GetRootWavelet()
  root_wavelet.CreateBlip().GetDocument().SetText("Hello there!")
  blip = context.GetBlipById(root_wavelet.GetRootBlipId())
  blip.GetDocument().AppendInlineBlip()

if __name__ == '__main__':
  webmain() # provide browsers something to look at 
  dummy = robot.Robot(ROBOT_NAME.capitalize(),
                      '1',
                      image_url='/public/%s.png' % ROBOT_NAME,
                      profile_url='_wave/profile.xml')
  dummy.RegisterHandler(events.WAVELET_SELF_ADDED,
                        OnSelfAdded)
  dummy.Run(debug=True)